﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using eShop.Utility;
using System.Collections;

namespace eShop.DataAccess
{
    public class OrderStatusReport
    {
        public string DateSelected
        {
            get;
            set;
        }

        public Int32 Success
        {
            get;
            set;
        }

        public Int32 Fail
        {
            get;
            set;
        }

        public Int32 Manual
        {
            get;
            set;
        }

    }
    public class OrderReport 
    {
        public string SaleYear
        {
            get;
            set;
        }
        public string SaleMonth
        {
            get;
            set;
        }
        public string SaleQuantity
        {
            get;
            set;
        }
    }

    public class ReportByDate
    {
        public string DateSelected
        {
            get;
            set;
        }
        public string key
        {
            get;
            set;
        }
        public string Val
        {
            get;
            set;
        }
    }

    public class ReportContainer
    {
        public string key
        {
            get;
            set;
        }
        public string Val
        {
            get;
            set;
        }
        
    }

    public class ReportDAO : DatabaseUtility
    {
        public static List<OrderReport> GetOrdersByMonth(DateTime startDate,DateTime endDate)
        {
            SqlCommand sqlCmd = new SqlCommand();
            SetCommandType(sqlCmd, CommandType.StoredProcedure, "spr_Report_Order_By_Month");
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddParameterToSqlCmd(sqlCmd, "@StartDate", SqlDbType.Date, 0, ParameterDirection.Input, startDate);
            AddParameterToSqlCmd(sqlCmd, "@EndDate", SqlDbType.Date, 0, ParameterDirection.Input, endDate);
            GenerateCollectionFromReader test = new GenerateCollectionFromReader(GenerateOrderReportCollectionFromReader);
            List<OrderReport> objCollection = ((List<OrderReport>)ExecuteReaderCmd(sqlCmd, test));

            return objCollection;
        }

        public static List<ReportContainer> GetRecentOrderSummary()
        {
            SqlCommand sqlCmd = new SqlCommand();
            SetCommandType(sqlCmd, CommandType.StoredProcedure, "spr_Report_Recent_Order_Status");
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            GenerateCollectionFromReader test = new GenerateCollectionFromReader(GenerateReportContainerCollectionFromReader);
            List<ReportContainer> objCollection = ((List<ReportContainer>)ExecuteReaderCmd(sqlCmd, test));

            return objCollection;
        }

        public static List<ReportByDate> GetOrderSummaryByMonth(DateTime startDate, DateTime endDate)
        {
            SqlCommand sqlCmd = new SqlCommand();
            SetCommandType(sqlCmd, CommandType.StoredProcedure, "spr_Report_GetOrderSummaryByMonth");
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddParameterToSqlCmd(sqlCmd, "@StartDate", SqlDbType.Date, 0, ParameterDirection.Input, startDate);
            AddParameterToSqlCmd(sqlCmd, "@EndDate", SqlDbType.Date, 0, ParameterDirection.Input, endDate);
            GenerateCollectionFromReader test = new GenerateCollectionFromReader(GenerateReportByDateContainerCollectionFromReader);
            List<ReportByDate> objCollection = ((List<ReportByDate>)ExecuteReaderCmd(sqlCmd, test));

            return objCollection;
        }

        public static List<ReportContainer> GetUsersByMonth(DateTime startDate, DateTime endDate)
        {
            SqlCommand sqlCmd = new SqlCommand();
            SetCommandType(sqlCmd, CommandType.StoredProcedure, "spr_Report_GetUsersByMonth");
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddParameterToSqlCmd(sqlCmd, "@StartDate", SqlDbType.Date, 0, ParameterDirection.Input, startDate);
            AddParameterToSqlCmd(sqlCmd, "@EndDate", SqlDbType.Date, 0, ParameterDirection.Input, endDate);
            GenerateCollectionFromReader test = new GenerateCollectionFromReader(GenerateReportContainerCollectionFromReader);
            List<ReportContainer> objCollection = ((List<ReportContainer>)ExecuteReaderCmd(sqlCmd, test));

            return objCollection;
        }

        public static IList GenerateOrderReportCollectionFromReader(IDataReader returnData)
        {
            //creating the instance of Employee collection
            List<OrderReport> entityColls = new List<OrderReport>();
            System.Globalization.DateTimeFormatInfo mfi = new System.Globalization.DateTimeFormatInfo();
            
            while (returnData.Read())
            {
            
                OrderReport entityCol = new OrderReport();
                entityCol.SaleMonth = returnData["SaleMonth"] == System.DBNull.Value ? String.Empty : mfi.GetAbbreviatedMonthName(Convert.ToInt32(returnData["SaleMonth"].ToString()));
                entityCol.SaleYear = returnData["SaleYear"] == System.DBNull.Value ? String.Empty : returnData["SaleYear"].ToString();
                entityCol.SaleQuantity = returnData["SaleQuantity"] == System.DBNull.Value ? String.Empty : returnData["SaleQuantity"].ToString();
                entityColls.Add(entityCol);
            }            
            return entityColls;
        }

        public static IList GenerateReportContainerCollectionFromReader(IDataReader returnData)
        {
            //creating the instance of Employee collection
            List<ReportContainer> entityColls = new List<ReportContainer>();
            
            while (returnData.Read())
            {

                ReportContainer entityCol = new ReportContainer();
                entityCol.key = returnData["key"] == System.DBNull.Value ? String.Empty : returnData["key"].ToString();
                entityCol.Val = returnData["Value"] == System.DBNull.Value ? String.Empty : returnData["Value"].ToString();
                
                entityColls.Add(entityCol);
            }
            return entityColls;
        }


        public static IList GenerateReportByDateContainerCollectionFromReader(IDataReader returnData)
        {
            //creating the instance of Employee collection
            List<ReportByDate> entityColls = new List<ReportByDate>();
            while (returnData.Read())
            {

                ReportByDate entityCol = new ReportByDate();
                entityCol.DateSelected = returnData["DateSelected"] == System.DBNull.Value ? String.Empty : returnData["DateSelected"].ToString();
                entityCol.key = returnData["key"] == System.DBNull.Value ? String.Empty : returnData["key"].ToString();
                entityCol.Val = returnData["Value"] == System.DBNull.Value ? String.Empty : returnData["Value"].ToString();

                entityColls.Add(entityCol);
            }
            return entityColls;
        }
    }
}
